package coin.bishi;

import java.util.Arrays;
import java.util.Comparator;
import java.util.List;
import java.util.stream.Collectors;

/**
 * @author coinyang
 * @date 2023/11/21 14:47
 */
public class 数字酋长 {
    /*  面试题
    1,二进制转换的1个数
    2,奇数在前偶数在后 还有排序
    3,最长公共字符串
    4,返回删除含关键字的集合
     */
    public static void main(String[] args) {

       int a = 555;
       int b = 555;

    }

    //1
    public static int binaryToDecimal(String binary) {
        int decimal = 0;
        int power = 1;
        for (int i = binary.length() - 1; i >= 0; i--) {
            int digit = Character.getNumericValue(binary.charAt(i));
            decimal += digit * power;
            power *= 2;
        }
        return decimal;
    }
    //2


        public static void two(String[] args) {
            Integer[] arr = {5, 2, 9, 1, 5, 6};
            Arrays.sort(arr, new Comparator<Integer>() {
                @Override
                public int compare(Integer o1, Integer o2) {
                    if (o1 % 2 == 0 && o2 % 2 != 0) {
                        return 1;
                    } else if (o1 % 2 != 0 && o2 % 2 == 0) {
                        return -1;
                    } else {
                        return o1.compareTo(o2);
                    }
                }
            });
            System.out.println(Arrays.toString(arr)); // [1, 5, 9, 5, 6, 2]
        }

    // 3

    public static String longestCommonString(String str1, String str2) {
        int maxLength = 0;
        int i = 0, j = 0;
        while (i < str1.length() && j < str2.length()) {
            if (str1.charAt(i) == str2.charAt(j)) {
                int length = 1;
                while (i + length < str1.length() && j + length < str2.length() && str1.charAt(i + length) == str2.charAt(j + length)) {
                    length++;
                }
                if (length > maxLength) {
                    maxLength = length;
                    int start = i + 1;
                    int end = i + length - 1;
                    return str1.substring(start, end);
                }
                i += length;
                j += length;
            } else {
                if (str1.charAt(i) < str2.charAt(j)) {
                    i++;
                } else {
                    j++;
                }
            }
        }
        return "";
    }

    //  4

    public static void four(String[] args) {
        List<String> list = Arrays.asList("Hello", "World", "Hello World", "Java", "Python");
        String keyword = "World";

        List<String> filteredList = list.stream()
                .filter(s -> !s.contains(keyword))
                .collect(Collectors.toList());

        System.out.println(filteredList); // 输出：[Hello, Java, Python]
    }
}
